package com;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        //连接服务器
        Socket server = new Socket("192.168.13.133", 8000);
        //启动线程
        new Thread(new Send(server)).start();
        new Thread(new Receive(server)).start();
        System.out.println("请输入昵称");
    }

    //接收信息
    static class Receive implements Runnable {
        private Socket server = null;
        private boolean flag = true;
        DataInputStream serverR = null;

        //构造函数
        public Receive(Socket server) {
            //初始化
            this.server = server;
            try {
                this.serverR = new DataInputStream(server.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        @Override
        public void run() {
            while (flag) {
                System.out.println(receive());
            }
        }

        private String receive() {
            String msg = null;
            try {
                System.out.println("接收信息成功！");
                byte[] bufArray = new byte[4096];//从服务器单次读取的最大数据
                int len = serverR.read(bufArray);
                if (len != -1) {
                    ByteBuffer data = ByteBuffer.allocate(len);
                    data.put(bufArray, 0, len);
                    System.out.println(data.array());
                }
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("接收信息失败！");
                flag = false;
            }
            return msg;
        }
    }


    //发送信息
    static class Send implements Runnable {
        private Socket server = null;
        private DataOutputStream serverW = null;
        private boolean flag = true;

        public Send(Socket server) {
            this.server = server;
            try {
                serverW = new DataOutputStream(server.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //线程体
        @Override
        public void run() {
            while (flag)
                send();
        }

        //发送函数
        private void send() {
            Scanner scanner = new Scanner(System.in);
            String msg = scanner.next();
            try {
                byte[] bytes = null;
                if ("1".equals(msg)) {
                    bytes = new byte[]{0x55, (byte) 0xAA, 0x06, (byte) 0xA1, 0x01, (byte) 0xA7};
                } else if ("2".equals(msg)) {
                    bytes = new byte[]{0x55, (byte) 0xAA, 0x06, (byte) 0xA1, 0x02, (byte) 0xA8};
                } else if ("3".equals(msg)) {
                    bytes = new byte[]{0x55, (byte) 0xAA, 0x07, (byte) 0xA2, 0x05, 0x00, (byte) 0xAD};
                }
                serverW.write(bytes);
                //serverW.writeUTF(msg);
                serverW.flush();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("发送信息失败！");
                flag = false;
            }

        }
    }

}
